import { BrowserWindowConstructorOptions, BrowserWindow } from 'electron'
import { windowOption } from '../../type/main/window'
export class CustomWindow {
  config: BrowserWindowConstructorOptions
  defaultWindowCfg: BrowserWindowConstructorOptions
  win: BrowserWindow | undefined
  winURL?: string | undefined
  options: windowOption
  constructor(options: windowOption) {
    this.defaultWindowCfg = {
      height: 563,
      useContentSize: true,
      width: 1000,
      autoHideMenuBar: true,
      webPreferences: {
        webSecurity: false,
        devTools: true,
        allowRunningInsecureContent: false,
        webgl: true,
        nodeIntegration: true,
        contextIsolation: false,
        defaultFontSize: 18,
        zoomFactor: 1
      }
    }
    this.config = Object.assign(this.defaultWindowCfg, options.windowCfg)
    this.options = options
    if (process.env.NODE_ENV === 'development') {
      console.log('process.env.NODE_ENV', process.env.NODE_ENV)
      this.winURL = options.pageRouter
        ? `${process.env.ELECTRON_RENDERER_URL}/#/${options.pageRouter}`
        : ''
    }
  }
  createWindow(): Promise<CustomWindow> {
    return new Promise((resolve) => {
      this.win = new BrowserWindow(this.config)
      resolve(this)
    })
  }
  load(url?: string, cb?: () => void) {
    if (url) {
      this.winURL = url
    }
    this.win?.loadURL(this.winURL as string).then((e) => {
      console.log('e', e)
      if (cb && typeof cb === 'function') {
        cb()
      }
    })
  }
  close() {
    this.win?.close()
  }
}

export class createWindow {}
// export default new CustomWindow()
